<?php

error_reporting(E_ALL ^ E_NOTICE);

if (!function_exists('exception')) {
    /**
     * 抛出验证异常
     * @param string $message
     * @throws Exception
     */
    function exception(string $message = '')
    {
        throw new \think\exception\ValidateException($message);
    }
}

if (!function_exists('D')) {
    /**
     * 实例化模型
     * @param string $name 路径分割用.隔开
     * @param string $prefix 模型前缀
     * @return object|\think\App
     */
    function D($name = '', $prefix = "app\\common\\model\\")
    {
        $name = str_replace(".", "\\", $name);
        $model = $prefix . $name . 'Model';
        return app($model);
    }
}
if (!function_exists('captcha')) {
    /**
     * 验证码方法
     * @param int $id
     * @param null $config
     * @return \think\Response
     * @throws Exception
     */
    function captcha($id = 0, $config = null): \think\Response
    {
        return \think\captcha\facade\Captcha::create($id, $config);
    }
}
if (!function_exists('captcha_src')) {
    /**
     * 验证码
     * @param $id
     * @return string
     */
    function captcha_src($id = 0): string
    {
        //<img src="{:captcha_src()}" style="cursor: pointer;" onclick="this.src=this.src+'?d='+Math.random();" />
        return \Think\Facade\Route::buildUrl('captcha/' . $id);
    }
}
if (!function_exists('captcha_check')) {
    /**
     * 校验验证码
     * @param string $value
     * @param int $id
     * @return bool
     */
    function captcha_check(string $value, $id = 0)
    {
        return \think\captcha\facade\Captcha::check($value, $id);
    }
}
if (!function_exists('html_out')) {
    /**
     * 还原转义html
     * @param $html
     * @return string
     */
    function html_out($html)
    {
        return \helper\Utils::htmlOut($html);
    }
}
if (!function_exists('cms_model')) {
    /**
     * 实例化cms模型
     * @param $tableName
     * @return mixed
     */
    function cms_model($tableName = null)
    {
        $model = "app\\common\\model\\cms\\DetailModel";
        if (empty($tableName)) return app($model);
        return app($model)->setSuffix($tableName);
    }
}
if (!function_exists('define_arr')) {
    /**
     * 提取数组指定键组装新数组
     * @param $arr
     * @param $fields
     * @return array
     * @throws Exception
     */
    function define_arr($arr, $fields)
    {
        $fields = is_string($fields) ? explode(',', $fields) : $fields;
        $result = [];
        foreach ($fields as $field) {
            $field = trim($field);
            if (!isset($arr[$field])) {
                $result[$field] = '';
            }
            $result[$field] = $arr[$field];
        }
        return $result;
    }
}
if (!function_exists('check_token')) {
    /**
     * 检测权限
     * @param string $name 权限名
     * @param string $cookieKey cookie键值
     * @param string[] $administrator
     * @return bool
     * @throws Exception
     */
    function check_token($name, $cookieKey = 'system_token', $administrator = ['1'])
    {
        $token = cookie($cookieKey);
        if (empty($token)) return false;
        \helper\JwtHelper::make()->verfiyToken($token);
        $payLoad = \helper\JwtHelper::make()->decodeData();
        if (!isset($payLoad['uid']) || !isset($payLoad['group_id'])) return false;
        //超级管理员可以操作任何权限
        if (in_array($payLoad['uid'], $administrator)) return true;
        return \helper\Auth::instance()->checkToken(strtolower($name), cookie($cookieKey));
    }
}
if (!function_exists('patch')) {
    /**
     * 获取patch内容
     * 获取patch内容
     * @param $patchSn
     * @return string
     * @throws Exception
     */
    function patch($patchSn)
    {
        $info = D('base.Patch')->where('patch_sn', $patchSn)->find();
        $content = '';
        if (!empty($info)) {
            switch ($info['patch_type']) {
                case '1': //单图碎片
                    $content = $info['patch_content'];
                    break;
                case '2': //多图碎片
                case '5': //多附件碎片
                case '6': //项目列表碎片
                    $content = json_decode($info['patch_content'], true);
                    break;
                case '3': //代码碎片
                case '4': //富文本碎片
                    $content = html_out($info['patch_content']);
                    break;
            }
        }
        return $content;
    }
}
if (!function_exists('dict')) {
    /**
     * 获取patch内容
     * 获取patch内容
     * @param $patchSn
     * @return string
     * @throws Exception
     */
    function dict($classCode)
    {
        $list = D('base.DictItem')->where('class_code', $classCode)->order('show_order asc')->select()->toArray();
        return $list;
    }
}

function sendMiniMessage($data){
    $message = [
        'template_id' => '8jSB2zWUjwEqdGV9c9RrRlSwRlmEH6LbLJ9u1Rk7oXY', // 所需下发的订阅模板id
        'touser' => $data['openid'],     // 接收者（用户）的 openid
        //'page' => $data['page'],
        'miniprogram_state'=>'trial',
        'data' => $data['msg']
    ];
    if(isset($data['page']) && $data['page']){
        $message['page'] = $data['page'];

    }
    $config = config('wechat.mini_program');
    $app = \EasyWeChat\Factory::miniProgram($config);
    $result = $app->subscribe_message->send($message);
    return $result;
}

/**
 * 房屋性质
 * @return array
 * @author bxm
 * @date 2022-06-15 21:56
 */
function houseAttrList(){
    $list = [
        [
            'id'     => 1,
            'name' => '毛坯',
        ],
        [
            'id'     => 2,
            'name' => '精装',
        ],
    ];
    return $list;
}
/**
 * 房屋结构
 * @return array
 * @author bxm
 * @date 2022-06-15 21:55
 */
function houseStructureList(){
    $list = [
        [
            'id'     => 1,
            'name' => '平层',
        ],
        [
            'id'     => 2,
            'name' => '叠层',
        ],
        [
            'id'     => 3,
            'name' => '别墅',
        ],
        [
            'id'     => 4,
            'name' => '商业',
        ],
    ];
    return $list;
}
/**
 * 汇款周期
 * @return array
 * @author bxm
 * @date 2022-06-15 21:53
 */
function paybackPeriodlist(){
    $list = [
        [
            'id'     => 1,
            'name' => '本周',
        ],
        [
            'id'     => 2,
            'name' => '本月',
        ],
        [
            'id'     => 3,
            'name' => '下月',
        ],
        [
            'id'     => 4,
            'name' => '长期',
        ],
        [
            'id'     => 5,
            'name' => '待放弃',
        ],
    ];
    return $list;
}

/**
 * 家装进度
 * @return array
 * @author bxm
 * @date 2022-06-15 21:50
 */
function decorateProgressList(){
    $list = [
        [
            'id'     => 1,
            'name' => '设计',
        ],
        [
            'id'     => 2,
            'name' => '签单',
        ],
        [
            'id'     => 3,
            'name' => '土建',
        ],
        [
            'id'     => 4,
            'name' => '室内拆改',
        ],
        [
            'id'     => 5,
            'name' => '改水电',
        ],
        [
            'id'     => 6,
            'name' => '铺砖',
        ],
        [
            'id'     => 7,
            'name' => '木工',
        ],
        [
            'id'     => 8,
            'name' => '油工',
        ],
    ];
    return $list;
}
function followList(){
    $list = [
        [
            'id'     => 1,
            'name' => '见面',
        ],
        [
            'id'     => 2,
            'name' => '微信',
        ],
        [
            'id'     => 3,
            'name' => '电话',
        ],

    ];
    return $list;
}
//极简，轻奢，美式，欧式，法式，新中式
/**
 * @return array
 * @author bxm
 * @date 2022-07-20 21:29
 * 装修风格
 */
function decorateStyleList(){
    $list = [
        [
            'id'     => 1,
            'text' => '极简',
        ],
        [
            'id'     => 2,
            'text' => '轻奢',
        ],
        [
            'id'     => 3,
            'text' => '美式',
        ],
        [
            'id'     => 4,
            'text' => '欧式',
        ],
        [
            'id'     => 5,
            'text' => '法式',
        ],
        [
            'id'     => 6,
            'text' => '新中式',
        ],

    ];
    return $list;
}
function getVueTree($data, $pId)
{
    $tree = [];
    foreach ($data as $k => $v) {
        if ($v['children'] == $pId) {
            $v['children'] = getVueTree($data, $v['id']);
            $p_data        = $v;
            if (!$v['children']) {
                unset($p_data['children']);
            }
            $tree[] = $p_data;
        }
    }
    return $tree;
}